package com.csmht.tododemo.interceptor;

import com.csmht.tododemo.pojo.entity.Role;
import com.csmht.tododemo.pojo.enumeration.RoleEnum;
import com.csmht.tododemo.until.BaseUnit;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Component
@Slf4j
@Order(2)
public class AdminInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {



        log.info("adminURL:{}",request.getRequestURI());

        if(request.getMethod().equals("OPTIONS")){
            return true;
        }

        Role role = BaseUnit.getCurrentRole();

        if(role.getRole() == RoleEnum.ADMIN) {
            return true;
        }

        response.setStatus(401);
        return false;
    }
}
